package com.ruoyi.zyx.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.ZyxDevice;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.zyx.domain.ZyxAllot;
import com.ruoyi.zyx.domain.ZyxCheck;
import com.ruoyi.zyx.domain.ZyxRecord;
import com.ruoyi.zyx.domain.ZyxTakecare;
import com.ruoyi.zyx.mapper.ZyxDeviceMapper;
import com.ruoyi.zyx.mapper.ZyxRecordMapper;
import com.ruoyi.zyx.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备基础信息Controller
 *
 * @author lhs
 * @date 2021-05-26
 */
@RestController
@RequestMapping("/zyx/device")
public class ZyxDeviceController extends BaseController
{
    @Autowired
    private IZyxDeviceService zyxDeviceService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IZyxTakecareService zyxTakecareService;
    @Autowired
    private IZyxCheckService zyxCheckService;
    @Autowired
    private IZyxAllotService zyxAllotService;
    @Autowired
    private ZyxDeviceMapper zyxDeviceMapper;
    @Autowired
    private IZyxRecordService zyxRecordService;
    /**
     * 查询设备基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('zyx:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZyxDevice zyxDevice)
    {
        startPage();
        List<ZyxDevice> list = zyxDeviceService.selectZyxDeviceList(zyxDevice);
        return getDataTable(list);
    }
    @GetMapping("/selectDeviceTj")
    public TableDataInfo selectDeviceTj(ZyxDevice zyxDevice)
    {
        startPage();
        zyxDevice.setDepartid(SecurityUtils.getLoginUser().getUser().getDeptId());
        List<ZyxDevice> list = zyxDeviceService.selectDeviceTj(zyxDevice);
        return getDataTable(list);
    }

    @GetMapping("/listAllDevice")
    public AjaxResult listAllDevice(ZyxDevice zyxDevice)
    {
        List<ZyxDevice> list = zyxDeviceMapper.selectZyxDeviceList(zyxDevice);
        return AjaxResult.success(list);
    }



    @GetMapping("/listnosel")
    public TableDataInfo listnosel(ZyxDevice zyxDevice)
    {
        zyxDevice.setDepartid(SecurityUtils.getLoginUser().getUser().getDeptId());
//        zyxDevice.setMemberid(SecurityUtils.getLoginUser().getUser().getUserId());
        startPage();
        List<ZyxDevice> list = zyxDeviceService.selectNoSelZyxDeviceList(zyxDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('zyx:device:export')")
    @Log(title = "设备基础信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ZyxDevice zyxDevice)
    {
        List<ZyxDevice> list = zyxDeviceService.selectZyxDeviceList(zyxDevice);
        ExcelUtil<ZyxDevice> util = new ExcelUtil<ZyxDevice>(ZyxDevice.class);
        return util.exportExcel(list, "设备基础信息数据");
    }

    /**
     * 获取设备基础信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('zyx:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(zyxDeviceService.selectZyxDeviceById(id));
    }

    /**
     * 新增设备基础信息
     */
    @PreAuthorize("@ss.hasPermi('zyx:device:add')")
    @Log(title = "设备基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZyxDevice zyxDevice)
    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        zyxDevice.setDevicestatus("1");//新增的都是正常的设备
        zyxDevice.setDepartid(SecurityUtils.getLoginUser().getUser().getDeptId());
        return toAjax(zyxDeviceService.insertZyxDevice(zyxDevice));
    }

    /**
     * 修改设备基础信息
     */
    @PreAuthorize("@ss.hasPermi('zyx:device:edit')")
    @Log(title = "设备基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZyxDevice zyxDevice)
    {
        return toAjax(zyxDeviceService.updateZyxDevice(zyxDevice));
    }

    /**
     * 删除设备基础信息
     */
    @PreAuthorize("@ss.hasPermi('zyx:device:remove')")
    @Log(title = "设备基础信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        if (zyxDeviceService.checkDeviceIsUse(ids))
        {
            return AjaxResult.error("设备存在未删除的业务信息，无法删除。");
        }

        return toAjax(zyxDeviceService.deleteZyxDeviceByIds(ids));
    }

    /**
     * 获取设备下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect( ZyxDevice zyxDevice)
    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        Long userId = loginUser.getUser().getUserId();
        List<TreeSelect> treeSelects=new ArrayList<>();

        List<ZyxDevice> devices = zyxDeviceMapper.selectZyxDeviceList(zyxDevice);
        if(null!=devices){
            for (ZyxDevice device : devices) {
                TreeSelect treeSelect=new TreeSelect();
                treeSelect.setId(device.getId());
                treeSelect.setLabel(device.getDevicemodel());
                treeSelects.add(treeSelect);
            }

        }

        return AjaxResult.success(treeSelects);
//        return AjaxResult.success("");
    }


    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('zyx:device:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ZyxDevice zyxDevice)
    {
        ZyxDevice zyxDeviceQuery= zyxDeviceService.selectZyxDeviceById(zyxDevice.getId());
        if(zyxDevice.getChangemode().equals("1")){//保养
            if(zyxDevice.getStatus().equals("1")){//添加
                ZyxTakecare zyxTakecare=new ZyxTakecare();
                zyxTakecare.setDepartid(zyxDeviceQuery.getDepartid());
                zyxTakecare.setDeviceid(zyxDevice.getId());
                zyxTakecare.setIswarn("N");
                zyxTakecare.setCreateTime(new Date());
                zyxTakecare.setCreateBy(SecurityUtils.getUsername());
                zyxTakecareService.insertZyxTakecare(zyxTakecare);
                zyxDevice.setIstake("1");
            }else{
                zyxTakecareService.delByDeviceId(zyxDevice.getId());
                zyxDevice.setIstake("0");
            }
        }else if(zyxDevice.getChangemode().equals("2")){//检定
            if(zyxDevice.getStatus().equals("1")){//添加
                ZyxCheck zyxCheck=new ZyxCheck();
                zyxCheck.setDepartid(zyxDeviceQuery.getDepartid());
                zyxCheck.setDeviceid(zyxDevice.getId());
                zyxCheck.setCreateTime(new Date());
                zyxCheck.setIswarn("N");
                zyxCheck.setCreateBy(SecurityUtils.getUsername());
                zyxCheckService.insertZyxCheck(zyxCheck);
                zyxDevice.setIscheck("1");
            }else{
                zyxCheckService.delByDeviceId(zyxDevice.getId());
                zyxDevice.setIscheck("0");
            }
        }else if(zyxDevice.getChangemode().equals("3")){//建档
            if(zyxDevice.getStatus().equals("1")){//添加
                ZyxRecord zyxRecord=new ZyxRecord();
                zyxRecord.setDepartid(zyxDeviceQuery.getDepartid());
                zyxRecord.setDeviceid(zyxDevice.getId());
                zyxRecord.setCreateTime(new Date());
                zyxRecord.setCreateBy(SecurityUtils.getUsername());
                zyxRecordService.insertZyxRecord(zyxRecord);
                zyxDevice.setIsrecord("1");
            }else{
                zyxRecordService.delByDeviceId(zyxDevice.getId());
                zyxDevice.setIsrecord("0");
            }
        }else if(zyxDevice.getChangemode().equals("4")){//调拨
            if(zyxDevice.getStatus().equals("1")){//添加
                ZyxAllot zyxAllot=new ZyxAllot();
                zyxAllot.setDepartid(zyxDeviceQuery.getDepartid());
                zyxAllot.setDeviceid(zyxDevice.getId());
                zyxAllot.setCreateTime(new Date());
                zyxAllot.setCreateBy(SecurityUtils.getUsername());
                zyxAllot.setIswarn("N");
                zyxAllot.setIswarn("0");
                zyxAllotService.insertZyxAllot(zyxAllot);
                zyxDevice.setIsallot("1");
            }else{
                zyxAllotService.delByDeviceId(zyxDevice.getId());
                zyxDevice.setIsallot("0");
            }
        }else if(zyxDevice.getChangemode().equals("5")){//建立核算运维
            if(zyxDevice.getStatus().equals("1")){//添加
                zyxDevice.setIsoperation("1");
                }else{
                zyxDevice.setIsoperation("0");
            }

        }
        zyxDeviceService.updateZyxDevice(zyxDevice);
        return  AjaxResult.success("");
    }
}
